package cn.billy.basic.controller;

import cn.billy.basic.dto.RegisterDto;
import cn.billy.basic.exception.BusinessException;
import cn.billy.basic.service.IVerifyCodeService;
import cn.billy.basic.utils.AjaxResult;
import cn.billy.org.domain.Department;
import cn.billy.org.domain.Shop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult graph(@PathVariable("key") String key){
        try {
            String Base64 = verifyCodeService.graph(key);
            System.out.println(Base64);
            return AjaxResult.me().setResultObj(Base64);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }

    }

    @PostMapping("/smsCode")
    public AjaxResult settlement(@RequestBody RegisterDto registerDto) {
        try {
            // 传入前端的数据
            verifyCodeService.smsCode(registerDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！");
        }
    }
    @PostMapping("/emailCode")
    public AjaxResult emailCode(@RequestBody RegisterDto RegisterDto){
        try {
            verifyCodeService.emailCode(RegisterDto);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
}
